import java.util.ArrayList;
import java.util.List;

public class Solution385 {

    public static class NestedInteger {

        Integer i;
        List<NestedInteger> list = new ArrayList<>();

        public NestedInteger() {

        }

        public NestedInteger(int value) {
            i = value;
        }

        public boolean isInteger() {
            return i == null;
        }

        public Integer getInteger() {
            return i;
        }

        public void setInteger(int value) {
            i = value;
        }

        public void add(NestedInteger ni) {
            list.add(ni);
        }

        public List<NestedInteger> getList() {
            return list;
        }
    }

    int cur=0;

    public NestedInteger deserialize(String s) {
        if (!s.startsWith("[")){
            return new NestedInteger(Integer.parseInt(s));
        }else{
            cur=1;
            return solve(s);
        }
    }

    private NestedInteger solve(String s) {
        NestedInteger nestedInteger=new NestedInteger();
        while (s.charAt(cur)!=']'){
            if (s.charAt(cur)==','){
                cur++;
                continue;
            }
            if (s.charAt(cur)=='['){
                cur++;
                nestedInteger.add(solve(s));
                cur++;
                continue;
            }
            StringBuilder stringBuilder=new StringBuilder();
            while (s.charAt(cur)!=']' && s.charAt(cur)!=','){
                stringBuilder.append(s.charAt(cur));
                cur++;
            }
            nestedInteger.add(new NestedInteger(Integer.parseInt(stringBuilder.toString())));
        }
        return nestedInteger;
    }

    public static void main(String[] args) {
        System.out.println(new Solution385().deserialize("[123,[456,[789]]]").getList().get(1).getList().get(1).getList().get(0).getInteger());
    }
}
